﻿using B.S.BaseData.ErrorCode;
using B.S.BaseData.Manage.Api.Read.Apilaction.Command.ProductModelCommand;
using B.S.BaseData.Manage.Api.Read.DTO;
using B.S.Passport.Domain;
using DAL;
using MediatR;
using Microsoft.Data.SqlClient;
using System.Data;
using System.Data.Common;

namespace B.S.BaseData.Manage.Api.Read.Apilaction.CoomandHandler.ProductModelCommand
{
    /// <summary>
    /// 批量查询
    /// </summary>
    public class ProductModelPhtoCommandHandler : IRequestHandler<ProductModelPhtoCommand, ApiReqult<List<ProductModel>>>
    {
        private readonly DBHelper bHelper;

        public ProductModelPhtoCommandHandler(DBHelper bHelper)
        {
            this.bHelper = bHelper;
        }
        /// <summary>
        /// 批量查询
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiReqult<List<ProductModel>>> Handle(ProductModelPhtoCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiReqult<List<ProductModel>>();
            try
            {
                var productIds = request.ProductId;
                if (productIds == null || productIds.Count == 0)
                {
                    result.Data = new List<ProductModel>();
                    result.Code = ApiEnum.查询成功;
                    result.Msg = "查询成功，无查询条件";
                    return result;
                }

                var idList = string.Join(",", productIds);
                var sql = $"SELECT * FROM ProductModel WHERE ProductId IN ({idList})";

                var productModels = await Task.Run(() => bHelper.GetList<ProductModel>(sql));
                result.Data = productModels;
                result.Code = ApiEnum.查询成功;
                result.Msg = "查询成功";
            }
            catch (System.Exception ex)
            {
                // 这里可以添加日志记录
                result.Code = ApiEnum.查询失败;
                result.Msg = $"查询失败: {ex.Message}";
            }
            return result;
        }
    }
}
